package meshGrammar;

import java.util.Vector;





public class MeshEdge{
	MeshFace face1;
	MeshFace face2;
	MeshNode n1;
	MeshNode n2;
	boolean checked;
	Object data;
	int id;
	public MeshEdge(MeshNode n1, MeshNode n2) {
		this.n1=n1;
		this.n2=n2;
	}
	public void attach(){
		MeshNode n1=getNode1();
		n1.getEdges().add(this);
		MeshNode n2=getNode2();
		n2.getEdges().add(this);
	}
	public void detach(){
		MeshNode n1=getNode1();
		n1.getEdges().remove(this);
		MeshNode n2=getNode2();
		n2.getEdges().remove(this);
	}
	
	public MeshNode getNode1(){
		return (MeshNode)n1;
	}
	public MeshNode getNode2(){
		return (MeshNode)n2;
	}
	public MeshFace getOtherFace(MeshFace f){
		if (f==face1)return face2;
		if (f==face2)return face1;
		return null;
	}
	public MeshNode getOtherNode(MeshNode n){
		if (n1==n)return (MeshNode)n2;
		if (n2==n)return (MeshNode)n1;
		return null;
	}
	public float getLength(){
		return Vec3D.dist(n1, n2);
	}
	public Vec3D getNormal(){
		Vector<Vec3D>normals=new Vector<Vec3D>();
		if (face1!=null){
			normals.add(face1.getNormal());
		}
		if (face2!=null){
			normals.add(face2.getNormal());
		}
		Vec3D f1=new Vec3D();
		for (int j=0;j<normals.size();j++){
			Vec3D n=normals.get(j);
			f1.add(n);
		}
		f1.normalize();
		return f1;
	}
	public Vec3D getNormalUnscaled(){
		Vector<Vec3D>normals=new Vector<Vec3D>();
		if (face1!=null){
			normals.add(face1.getNormalUnscaled());
		}
		if (face2!=null){
			normals.add(face2.getNormalUnscaled());
		}
		Vec3D f1=new Vec3D();
		for (int j=0;j<normals.size();j++){
			Vec3D n=normals.get(j);
			f1.add(n);
		}
		return f1;
	}
	public MeshFace getFace(MeshNode n){
		if (n1==n)return face1;
		if (n2==n)return face2;
		return null;
	}
	public void setFace(MeshNode n,MeshFace face){
		if (n1==n){
			face1=face;
		}
		else if(n2==n){
			face2=face;
		}
		else{
			System.out.println("no node");
		}
	}
	public boolean isChecked() {
		return checked;
	}
	public void setChecked(boolean checked) {
		this.checked = checked;
	}
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public int getId() {
		// TODO Auto-generated method stub
		return id;
	}
	public void setId(int id) {
		// TODO Auto-generated method stub
		this.id=id;
	}
	public MeshFace getFace1() {
		return face1;
	}
	public void setFace1(MeshFace face1) {
		this.face1 = face1;
	}
	public MeshFace getFace2() {
		return face2;
	}
	public void setFace2(MeshFace face2) {
		this.face2 = face2;
	}
	public Vec3D getCenter(){
		Vec3D c=Vec3D.add(n1, n2);
		c.mult(0.5f);
		return c;
	}

}
